package com.excesys.exsecs.scada.controller;


import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.excesys.exsecs.common.core.util.R;
import com.excesys.exsecs.common.log.annotation.SysLog;
import com.excesys.exsecs.scada.entity.ScadaSystem;
import com.excesys.exsecs.scada.mapper.ScadaSystemMapper;
import com.excesys.exsecs.scada.service.ScadaSystemService;
import com.excesys.exsecs.common.excel.annotation.ResponseExcel;
import com.excesys.exsecs.scada.task.SystemTask;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.http.HttpHeaders;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * 系统参数
 *
 * @author tzh
 * @date 2024-12-09 10:43:01
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/scadaSystem" )
@Tag(description = "scadaSystem" , name = "系统参数管理" )
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class ScadaSystemController {

	private final ScadaSystemService scadaSystemService;
	private final ScadaSystemMapper scadaSystemMapper;

	private final SystemTask systemTask;

	/**
	 * 分页查询
	 * @param page 分页对象
	 * @param scadaSystem 系统参数
	 * @return
	 */
	@Operation(summary = "分页查询" , description = "分页查询" )
	@GetMapping("/page" )
	//@PreAuthorize("@pms.hasPermission('exsecs_scadaSystem_view')" )
	public R getScadaSystemPage(@ParameterObject Page page, @ParameterObject ScadaSystem scadaSystem) {
		LambdaQueryWrapper<ScadaSystem> wrapper = Wrappers.lambdaQuery();
		return R.ok(scadaSystemService.page(page, wrapper));
	}

	@Operation(summary = "详情接口" , description = "详情接口" )
	@GetMapping("/{id}" )
	public R getById(@PathVariable Long id){
		ScadaSystem system = scadaSystemService.getById(id);
		JSONObject prop = system.getProperties();
		system.setUnit(prop.getString("unit"));
		system.setDatatype(prop.getString("datatype"));
		system.setIdentifier(prop.getString("identifier"));
		system.setDescription(prop.getString("description"));
		return R.ok(system,"详情");
	}


	/**
	 * 查询树
	 * @param
	 * @return R
	 */
	@Operation(summary = "查询树" , description = "查询树" )
	@GetMapping("/tree" )
	//@PreAuthorize("@pms.hasPermission('exsecs_scadaSystem_tree')" )
	public R getById(@ParameterObject ScadaSystem scadaSystem) {
		List<Tree<Long>> treeSystem = scadaSystemService.systemTree(scadaSystem);
		return R.ok(treeSystem,"系统参数树");
	}

	/**
	 * 新增系统参数
	 * @param scadaSystem 系统参数
	 * @return R
	 */
	@Operation(summary = "新增系统参数" , description = "新增系统参数" )
	@SysLog("新增系统参数")
	@PostMapping
	//@PreAuthorize("@pms.hasPermission('exsecs_scadaSystem_add')" )
	public R save(@RequestBody ScadaSystem scadaSystem) {
		if(ObjectUtil.isNull(scadaSystem.getType())){
			return R.failed("type为空");
		}
		if(StringUtils.isEmpty(scadaSystem.getSystemName()) || scadaSystem.getSystemName().length()==0){
			return R.failed("系统参数名不能为空");
		}
		//新增系统参数的情况
		if(scadaSystem.getType().equals("1")){
			if(StringUtils.isEmpty(scadaSystem.getIdentifier()) || scadaSystem.getIdentifier().length()==0){
				return R.failed("系统参数标识不能为空");
			}
			//判断是否重复
			List<Map<String, String>> ifdupl = scadaSystemMapper.ifdupl();
			if(ifdupl.size()>0 && ObjectUtil.isNotEmpty(ifdupl.get(0))){
				for (int i = 0; i < ifdupl.size(); i++) {
					if(ifdupl.get(i).get("name").equals(scadaSystem.getSystemName()) ||
							ifdupl.get(i).get("identifier").equals(scadaSystem.getIdentifier())){
						return R.failed("系统参数已存在");
					}
				}
			}
			return R.ok(scadaSystemService.saveSystem(scadaSystem),"新增系统参数");
		}
		//新增文件夹的情况
		if(scadaSystem.getType().equals("0")) {
			return R.ok(scadaSystemService.save(scadaSystem), "新增文件夹");
		}
		return R.failed("新增失败");
	}

	/**
	 * 修改系统参数
	 * @param scadaSystem 系统参数
	 * @return R
	 */
	@Operation(summary = "修改系统参数" , description = "修改系统参数" )
	@SysLog("修改系统参数" )
	@PutMapping
	//@PreAuthorize("@pms.hasPermission('exsecs_scadaSystem_edit')" )
	public R updateById(@RequestBody ScadaSystem scadaSystem) {
		Map map = new HashMap<>();
		map.put("name",scadaSystem.getSystemName());
		map.put("identifier",scadaSystem.getIdentifier());
		map.put("unit",scadaSystem.getUnit());
		map.put("datatype",scadaSystem.getDatatype());
		map.put("description",scadaSystem.getDescription());
		JSONObject jsonObject = new JSONObject(map);
		scadaSystem.setProperties(jsonObject);
		return R.ok(scadaSystemService.updateById(scadaSystem));
	}

	/**
	 * 通过id删除系统参数
	 * @param ids id列表
	 * @return R
	 */
	@Operation(summary = "通过id删除系统参数" , description = "通过id删除系统参数" )
	@SysLog("通过id删除系统参数" )
	@DeleteMapping
	//@PreAuthorize("@pms.hasPermission('exsecs_scadaSystem_del')" )
	public R removeById(@RequestBody Long[] ids) {
		return R.ok(scadaSystemService.removeBatchByIds(CollUtil.toList(ids)));
	}


	/**
	 * 导出excel 表格
	 * @param scadaSystem 查询条件
	 * @param ids 导出指定ID
	 * @return excel 文件流
	 */
	@ResponseExcel
	@GetMapping("/export")
	//@PreAuthorize("@pms.hasPermission('exsecs_scadaSystem_export')" )
	public List<ScadaSystem> export(ScadaSystem scadaSystem,Long[] ids) {
		return scadaSystemService.list(Wrappers.lambdaQuery(scadaSystem).in(ArrayUtil.isNotEmpty(ids), ScadaSystem::getId, ids));
	}

	/**
	 * 测试
	 */
	@GetMapping("/test")
	public R test(){
		systemTask.tempHourFlow();
		return R.ok(null,"tempHourFlow()定时任务");
	}
}
